home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / Macintosh Easy Open 1.1.1 / Documentation / Developer / PowerPC / LowerToUpperCaseScrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  9.2 KB  |  283 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        LowerToUpperCaseScrap.c 
  3.  
  4.     Contains:    Source to a FAT scrap translator that converts from lower to upper case
  5.                 The source type is always 'TEXT' and the destination type is 'UPPR'.
  6.     
  7.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  8. */
  9.  
  10. #include <Types.h>
  11. #include <MixedMode.h>
  12. #include <Traps.h>
  13. #include <Errors.h>
  14. #include <Memory.h>
  15. #include <Components.h>
  16. #include "TranslationExtensions.h"
  17.  
  18. //___________________________________________________________________________________________________________
  19. //
  20. // Structures
  21. //
  22.  
  23. // Our magic cookie to tell if our translation list is initialized or not
  24. #define kModificationDateMagicCookie    0x00000001
  25.  
  26. // Set up a fixed scrap translation list structure for easy access
  27. #if defined(powerc) || defined (__powerc)
  28. #pragma options align=mac68k
  29. #endif
  30. struct OurTranslationList
  31. {
  32.     unsigned long    modDate;
  33.     unsigned long    groupCount;
  34.     unsigned long    sourceCount;
  35.     unsigned long    sourceEntrySize;
  36.     ScrapTypeSpec    sourceType;
  37.     unsigned long    destinationCount;
  38.     unsigned long    destinationEntrySize;
  39.     ScrapTypeSpec    desinationType;
  40. };
  41. #if defined(powerc) || defined(__powerc)
  42. #pragma options align=reset
  43. #endif
  44.  
  45. typedef struct OurTranslationList OurTranslationList;
  46. typedef OurTranslationList *OurTranslationListPtr, **OurTranslationListHandle;
  47.  
  48. //___________________________________________________________________________________________________________
  49. //
  50. // DoGetScrapTranslationList
  51. //
  52. // This routine is called by the Translation Manager when it needs to find out what formats the scrap
  53. // translator translates between.
  54. //
  55. // Enter:    self    Instance of this translator
  56. //            list    Handle to scrap translation list provided by this translator earlier, or empty handle
  57. //                    if never set before
  58. //
  59. // Exit:    list    Handle to scrap translation list filled in my this routine
  60. //            returns    Any errors that might occur
  61. //
  62. pascal ComponentResult DoGetScrapTranslationList(ComponentInstance             self, 
  63.                                                  ScrapTranslationListHandle    list)
  64. {
  65. #pragma unused(self)
  66.     OurTranslationListHandle    ourList = (OurTranslationListHandle) list;        // Easier to reference
  67.     OSErr                        result = noErr;
  68.  
  69.     // The list never changes here - so see if it's been initialized once by looking
  70.     // for our magic cookie
  71.     if (((*ourList)->modDate) != kModificationDateMagicCookie)
  72.         {
  73.         // Resize the handle to fit our entry
  74.         SetHandleSize((Handle)ourList, sizeof(OurTranslationList));
  75.         if ((result = MemError()) == noErr)
  76.             {
  77.             // Stuff all the values into our fixed list
  78.             (*ourList)->modDate = kModificationDateMagicCookie;                    // Magic cookie
  79.             (*ourList)->groupCount = 1;                                            // Number groups = 1
  80.             (*ourList)->sourceCount = 1;                                        // Only 1 source type
  81.             (*ourList)->sourceEntrySize = sizeof(ScrapTypeSpec);                // Currently always sizeof(ScrapTypeSpec)
  82.             (*ourList)->sourceType.format = 'TEXT';                                // Source type is always 'TEXT'
  83.             (*ourList)->sourceType.hint = 0;                                    // Don't care about this
  84.             (*ourList)->destinationCount = 1;                                    // Only 1 destination type
  85.             (*ourList)->destinationEntrySize = sizeof(ScrapTypeSpec);            // Currently always sizeof(ScrapTypeSpec)
  86.             (*ourList)->desinationType.format = 'UPPR';                            // Destination type is always 'UPPR'
  87.             (*ourList)->desinationType.hint = 0;                                // Don't care about this
  88.             }        
  89.         }
  90.         
  91.     return result;
  92. }
  93.  
  94. //___________________________________________________________________________________________________________
  95. //
  96. // DoIdentifyScrap
  97. //
  98. // This routine is called to identify a scraps contents.  In this case, just check to see if the scrap
  99. // is type TEXT, and if it is, then we know it's TEXT.
  100. //
  101. // Enter:    self        Instance of this translator
  102. //            dataPtr        Pointer to data to identify
  103. //            dataLength    Size of data referenced by dataPtr
  104. //            dataFormat    Provided format for the data referenced by dataPtr
  105. //
  106. // Exit:    dataFormat    Changed format (if applicable)
  107. //            returns        noErr is scrap contents are TEXT, otherwise noTypeErr
  108. //
  109. pascal ComponentResult DoIdentifyScrap(ComponentInstance    self, 
  110.                                        const void*             dataPtr, 
  111.                                        Size                 dataLength, 
  112.                                        ScrapType*             dataFormat)
  113. {
  114. #pragma unused(self)
  115. #pragma unused(dataPtr)
  116. #pragma unused(dataLength)
  117.     OSErr    result = noErr;
  118.     
  119.     // We only know TEXT.  See if the format is that.  If it isn't then we don't translate
  120.     if (*dataFormat != 'TEXT')
  121.         result = noTypeErr;
  122.  
  123.     return result;
  124. }
  125.  
  126. //___________________________________________________________________________________________________________
  127. //
  128. // DoTranslateScrap
  129. //
  130. // This routine is called to perform a scrap translation.  It is only called if the source type is TEXT and
  131. // if DoIdentifyScrap was called as was able to confirm that.  Also, it is only called if the destination type
  132. // is UPPR.
  133. //
  134. // This specific translation converts all lower case characters to upper case.  Leaves the rest alone.
  135. //
  136. // Enter:    self            Instance of this translator
  137. //            progressRefNum    Reference number to progress dialog
  138. //            srcDataPtr        Pointer to source data
  139. //            srcDataLength    Length of data pointed to by srcDataPtr
  140. //            srcType            Type of data pointed to by srcDataPtr (provided via DoIdentifyScrap)
  141. //            srcTypeHint        Hint for data pointed to by srcDataPtr (from ScrapTranslationList)
  142. //            dstData            Handle to place translated destination data into
  143. //            dstType            Type of data to translate srcDataPtr into
  144. //            dstTypeHint        Hint for data to translate to (from ScrapTranslationList)
  145. //
  146. // Exit:    dstData            Handle resized and filled in with translated contents
  147. //            returns            Any errors that might occur.
  148. //            
  149. pascal ComponentResult DoTranslateScrap(ComponentInstance     self, 
  150.                                         TranslationRefNum     progressRefNum,
  151.                                         const void*            srcDataPtr, 
  152.                                         Size                 srcDataLength, 
  153.                                         ScrapType             srcType, 
  154.                                         long                 srcTypeHint,
  155.                                         Handle                 dstData, 
  156.                                         ScrapType             dstType, 
  157.                                         long                 dstTypeHint)
  158. {
  159. #pragma unused(self)
  160. #pragma unused(srcType)
  161. #pragma unused(srcTypeHint)
  162. #pragma unused(dstType)
  163. #pragma unused(dstTypeHint)
  164.  
  165.     OSErr    result;
  166.     char*    currentCharacter;
  167.     char*    lastCharacter;
  168.     char*    writeCharacter;
  169.     short    count;
  170.     Boolean    canceled = false;
  171.     
  172.     // No advertisement.  I'm too lazy to create a PICT
  173.     SetTranslationAdvertisement(progressRefNum, nil);
  174.     
  175.     // Resize the destination handle to fit all the translated contents
  176.     SetHandleSize(dstData, srcDataLength);
  177.     if ((result = MemError()) == noErr)
  178.         {
  179.         // Give me a pointer to the first character and the last character in the source buffer
  180.         currentCharacter = (char*) srcDataPtr;
  181.         lastCharacter = currentCharacter + srcDataLength;
  182.         
  183.         // Give me a pointer to the first character in the destination buffer
  184.         // (lock it down first because stupid update may move memory)
  185.         HLock(dstData);
  186.         writeCharacter = (char *)*dstData;
  187.         
  188.         // Keep a count for the progress dialog
  189.         count = 0;
  190.         
  191.         // Loop through each of the characters in the buffer
  192.         while ((currentCharacter <= lastCharacter) && !canceled)
  193.             {
  194.             // Are we in the lower case ASCII range?
  195.             if (('a' <= *currentCharacter) && (*currentCharacter <= 'z'))
  196.                 {
  197.                 *writeCharacter = (*currentCharacter - ('a' - 'A'));
  198.                 }
  199.             else
  200.                 *writeCharacter = *currentCharacter;
  201.             
  202.             // Increment the progress bar
  203.             UpdateTranslationProgress(progressRefNum, ++count * (100.00 / srcDataLength), &canceled);
  204.             
  205.             // Next…
  206.             currentCharacter++;
  207.             writeCharacter++;
  208.             }
  209.             
  210.         // Let it wiggle, see it squiggle
  211.         HUnlock(dstData);
  212.         }
  213.     
  214.     return result;
  215. }
  216.  
  217. //___________________________________________________________________________________________________________
  218. //
  219. // DoGetFileTranslationList
  220. //
  221. // Stub required by library link
  222. //
  223. pascal ComponentResult DoGetFileTranslationList(ComponentInstance             self, 
  224.                                                 FileTranslationListHandle    translationList)
  225. {
  226. #pragma unused(self)        
  227. #pragma unused(translationList)        
  228.     return paramErr;
  229. }
  230.  
  231. //___________________________________________________________________________________________________________
  232. //
  233. // DoIdentifyFile
  234. //
  235. // Stub required by library link
  236. //
  237. pascal ComponentResult DoIdentifyFile(ComponentInstance self, const FSSpec *theDoc, FileType *docKind)
  238. {
  239. #pragma unused(self)
  240. #pragma unused(theDoc)
  241. #pragma unused(docKind)
  242.     return paramErr;
  243. }
  244.  
  245. //___________________________________________________________________________________________________________
  246. //
  247. // DoTranslateFile
  248. //
  249. // Stub required by library link
  250. //
  251. pascal ComponentResult DoTranslateFile(ComponentInstance self, TranslationRefNum progressRefNum,
  252.                                             const FSSpec* srcDoc, FileType srcType, long srcTypeHint, 
  253.                                             const FSSpec* dstDoc, FileType dstType, long dstTypeHint)
  254. #pragma unused(self)                        /* no translation is actually done */
  255. #pragma unused(progressRefNum)
  256. #pragma unused(srcDoc)
  257. #pragma unused(srcType)
  258. #pragma unused(srcTypeHint)
  259. #pragma unused(dstDoc)
  260. #pragma unused(dstType)
  261. #pragma unused(dstTypeHint)
  262.     return paramErr;
  263. }
  264.  
  265. //___________________________________________________________________________________________________________
  266. //
  267. // DoGetTranslatedFilename
  268. //
  269. // Stub required by library link
  270. //
  271. pascal ComponentResult DoGetTranslatedFilename(ComponentInstance     self,
  272.                                                FileType                dstType,
  273.                                                long                    dstTypeHint,
  274.                                                FSSpec*                 theDocument)
  275. {
  276. #pragma unused(self);
  277. #pragma unused(dstType);
  278. #pragma unused(dstTypeHint);
  279. #pragma unused(theDocument);
  280.     return paramErr;
  281. }
  282.